package daoimpl

import (
	"log"
	"time"
	"ustcLab/conf"
	"ustcLab/server/dao/bean"
)

// select
func QueryMonitorInfo() (int, []*bean.TMonitorInfo, error) {
	var monitorInfos []*bean.TMonitorInfo
	count, err := GetInstance().FindAndCount(&monitorInfos)
	if err != nil {
		log.Fatalln("[QueryMonitorInfo] fail to find monitor info, err: ", err)
		return 0, nil, err
	}
	log.Printf("[QueryMonitorInfo] query monitorInfos success. count = %d\n", count)
	return int(count), monitorInfos, nil
}

// 实时数据、modify_time更新
func ModifyMonitorInfo(cond *bean.TMonitorInfo, now time.Time,
	bp, hr, breathe, spo2, sao2, o2, n2o float64) (int, error) {

	res, err := GetInstance().Get(cond)
	if err != nil {
		log.Fatalln("[ModifyMonitorInfo] err: ", err)
		return 0, err
	}
	if !res {
		log.Fatalf("[ModifyMonitorInfo] %s\n", conf.ErrorDatabaseNotExists)
		return 0, conf.String2Error[conf.ErrorDatabaseNotExists]
	}
	affected, err := GetInstance().Update(&bean.TMonitorInfo{
		FBloodPressureLower: bp,
		FBloodPressureUpper: bp + 10,
		FBreathe:            breathe,
		FHeartRadio:         hr,
		FModifyTime:         now,
		FSpo2:               spo2,
		FSao2:               sao2,
		FN2o:                n2o,
		FO2:                 o2,
	}, cond)

	if err != nil {
		log.Fatalln("[ModifyMonitorInfo] err: ", err)
	}
	return int(affected), err
}
